<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 * @author   howe <howe1995@163.com>
 */

namespace App\Controller\Api\V1;

use Exception;
use Fastknife\Exception\ParamException;
use Fastknife\Service\BlockPuzzleCaptchaService;
use Fastknife\Service\ClickWordCaptchaService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;

use function Hyperf\Config\config;

#[Controller(prefix: '/api/v1/system/')]
class SystemController extends CController
{
    /**
     * 获取验证码
     */
    #[RequestMapping(path: 'captcha/get', methods: 'post')]
    public function get(): array
    {
        try {
            $service = $this->getCaptchaService();
            $data = $service->get();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->result($data);
    }

    /**
     * 一次验证
     */
    #[RequestMapping(path: 'captcha/check', methods: 'post')]
    public function check(): array
    {
        $params = $this->request->all();
        try {
            $this->validate($params, [
                'token' => 'bail|required',
                'pointJson' => 'required',
            ]);
            $service = $this->getCaptchaService();
            $service->check($params['token'], $params['pointJson']);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->result([]);
    }

    /**
     * 二次验证
     */
    #[RequestMapping(path: 'captcha/verification', methods: 'post')]
    public function verification(): array
    {
        $params = $this->request->all();
        try {
            $this->validate($params, [
                'token' => 'bail|required',
                'pointJson' => 'required',
            ]);
            $service = $this->getCaptchaService();
            $service->verification($params['token'], $params['pointJson']);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->result([]);
    }

    protected function getCaptchaService(): BlockPuzzleCaptchaService|ClickWordCaptchaService
    {
        $captchaType = $this->request->post('captchaType', null);
        $config = config('captcha');
        return match ($captchaType) {
            'clickWord' => new ClickWordCaptchaService($config),
            'blockPuzzle' => new BlockPuzzleCaptchaService($config),
            default => throw new ParamException('captchaType参数不正确！'),
        };
    }

    private function result($data): array
    {
        return [
            'error' => false,
            'repCode' => '0000',
            'repData' => $data,
            'repMsg' => null,
            'success' => true,
        ];
    }

    private function error($msg): array
    {
        return [
            'error' => true,
            'repCode' => '6111',
            'repData' => null,
            'repMsg' => $msg,
            'success' => false,
        ];
    }
}
